package com.xiaotu.spider.maoyan.pipeline;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;
import com.xiaotu.common.model.MaoYanMovieCityCinemaBoxModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.spider.APipeline;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

/***
 * 
 * @author Mr.zhang
 *
 */
@Component("MaoYanMovieCityCinemaBoxPipeline")
public class MaoYanMovieCityCinemaBoxPipeline extends APipeline {

	@Override
	protected void doProcess(ResultItems resultItems, Task task) {
		
		
		List<MaoYanMovieCityCinemaBoxModel> cityList = resultItems.get(MaoYanMovieCityCinemaBoxModel.class.getName());
        if (cityList != null && !cityList.isEmpty())
        {
            //String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_AREA_YYYYMMDD);
        	String key = "MAOYAN_MOVIE_AREA_BOX_0";
            cacheHandler.hMSet(key, cityList.stream().collect(Collectors.toMap(mov -> mov.getMaxdate()+"_"+mov
                    .getDataId() + SepratorUtil.SEP_UNDERLINE + mov.getAreaName()+"_"+ mov.getId(), mov -> mov)));
            //cacheHandler.setCacheTimeout(key);
        }
        
        List<MaoYanMovieCityCinemaBoxModel> companyList = resultItems.get(MaoYanMovieCityCinemaBoxModel.class.getName() +
                Constants.PageDataType.MOVIE_COMPANY);
        if (companyList != null && !companyList.isEmpty())
        {
            //String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_COMPANY_YYYYMMDD);
        	String key = "MAOYAN_MOVIE_COMPANY_BOX_0";
            cacheHandler.hMSet(key, companyList.stream().collect(Collectors.toMap(mov -> mov.getMaxdate()+"_"+mov
                    .getDataId() + SepratorUtil.SEP_UNDERLINE + mov.getAreaName() +"_"+ mov.getId(), mov -> mov)));
            //cacheHandler.setCacheTimeout(key);
        }
        
	}
	
}
